package svc

import (
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"go-zero-real/api/demo/internal/config"
	"log"
)

type ServiceContext struct {
	Config     config.Config
	DB         sqlx.SqlConn
	RedisModel *redis.Redis
}

func NewServiceContext(c config.Config) *ServiceContext {
	// 构建 redis 配置
	redisConf := redis.RedisConf{
		Host: c.Redis.Host,
		Pass: c.Redis.Pass,
		Type: "node", // 或者 "cluster" 取决于你的 Redis 类型
	}

	// 初始化 Redis 客户端，处理 error
	redisClient, err := redis.NewRedis(redisConf)
	if err != nil {
		log.Fatalf("failed to initialize redis: %v", err)
	}

	log.Println("连接redis和mysql成功")

	return &ServiceContext{
		Config:     c,
		DB:         sqlx.NewMysql(c.DataSource), // 初始化数据库连接
		RedisModel: redisClient,
	}
}
